package com.song.tools.mvc.controller;

import com.song.tools.core.beans.response.ResultVo;
import com.song.tools.log.systemlog.annotation.SysLog;
import com.song.tools.mvc.request.BatchIdRequest;
import com.song.tools.mvc.request.IdRequest;
import com.song.tools.mvc.request.PageRequest;
import com.song.tools.mvc.response.PageResponse;
import com.song.tools.mvc.service.AggregationService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

/**
 * 聚合（CRUD+POI） 控制器的基类（默认实现）
 *
 *
 * @author song tools
 * @since 2024-06-25
 */
public abstract class SuperAggregationController<Service extends AggregationService<PrimaryKey, QueryRequest, QueryResponse, SaveRequest, SaveResponse, UpdateRequest, UpdateResponse, DeleteResponse, HttpServletResponse>,
        QueryRequest, QueryResponse, SaveRequest, SaveResponse, UpdateRequest, UpdateResponse, PrimaryKey extends Serializable, DeleteResponse>
        implements CrudController<QueryRequest, QueryResponse, SaveRequest, SaveResponse, UpdateRequest, UpdateResponse, PrimaryKey, DeleteResponse>, PoiController<QueryRequest> {

    /**
     * 业务服务实例
     */
    @Autowired
    private Service service;


    /**
     * 分页查询
     *
     * @param request 请求
     * @return 分页响应
     */
    @Override
    @SysLog("分页查询")
    public ResultVo<PageResponse<QueryResponse>> listPages(PageRequest<QueryRequest> request) {
        request.defaultPage();
        return ResultVo.success(service.listPages(request));
    }

    /**
     * 查询信息（不分页）
     *
     * @param request 请求
     * @return PageResponse 响应
     */
    @Override
    @SysLog("查询信息（不分页）")
    public ResultVo<List<QueryResponse>> list(QueryRequest request) {
        return ResultVo.success(service.list(request));
    }

    /**
     * 查询详细信息
     *
     * @param request 请求
     * @return 响应
     */
    @Override
    @SysLog("查询详细信息")
    public ResultVo<QueryResponse> getDetail(IdRequest<PrimaryKey> request) {
        return ResultVo.success(service.getOneByUniqueKey(request.getId(), "id"));
    }

    /**
     * 新增
     *
     * @param request 请求
     * @return 响应
     */
    @SysLog("新增")
    @Override
    public ResultVo<SaveResponse> save(SaveRequest request) {
        return ResultVo.success(service.save(request));
    }

    /**
     * 修改
     *
     * @param request 请求
     * @return 响应
     */
    @SysLog("修改")
    @Override
    public ResultVo<UpdateResponse> update(UpdateRequest request) {
        return ResultVo.success(service.update(request));
    }

    /**
     * 删除
     *
     * @param request 请求
     * @return 响应
     */
    @SysLog("删除")
    @Override
    public ResultVo<DeleteResponse> delete(BatchIdRequest<PrimaryKey> request) {
        return ResultVo.success(service.deleteByPrimaryKeys(request));
    }

    /**
     * 导入
     */
    @SysLog("导入")
    @Override
    public ResultVo<Boolean> importExcel(@RequestPart("file") MultipartFile file) {
        service.importExcel(file);
        return ResultVo.success();
    }

    /**
     * 获取导入模版
     */
    @Override
    @SysLog("获取导入模版")
    public void importTpl(HttpServletResponse response) throws IOException {
        service.importTpl(response);
    }

    /**
     * 导出
     */
    @Override
    @SysLog("导出")
    public void exportExcel(HttpServletResponse response, QueryRequest request) throws IOException {
        service.export(response, request);
    }
}
